<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 公用继承模型
 *
 * @author www.dingwei.cn
 */

class MY_Model extends CI_Model{
	
 	/**
     * 获取数据表中记录条数
     * 
     * @param	string		$table		表名称
	 * @param	object 		$db			数据模型对象
	 * @return	array					
     */
    public function count($table, $db = NULL, $reset = true, $colun = ''){
    	
    	$db = $db == NULL ? $this->db : $db;
    	if($colun){
    		$db->setCountStirng($colun);
    	}
		return $db->count_all_results($table, $reset);  	
    }	
    
	/**
     * 删除表数据
     * 
     * @param	string		$table		表名称
	 * @param	object 		$db			数据模型对象
	 * @return	boolean	
     */
    public function delete($table, $db = NULL){
    	
    	$db = $db == NULL ? $this->db : $db;
		return $db->delete($table);   	
    }
	
	/**
     * 获取数据表中的一条数据
     * 
     * @param	string		$table		表名称
	 * @param	object 		$db			数据模型对象
	 * @return	array					
     */
    public function getOne($table, $db = NULL){
    	
    	$db = $db == NULL ? $this->db : $db;
		$tA = $db->get($table)->row_array();  	
		if( ! $tA) return array();
		return $tA;
    }	
    
    /**
     * 获取数据表中多条数据
     * 
     * @param	string		$table		表名称
	 * @param	object 		$db			数据模型对象
	 * @return	array					
     */
    public function getAll($table, $db = NULL){
    	
    	$db = $db == NULL ? $this->db : $db;
		return $db->get($table)->result_array();  	
    }
    
	/**
	 * 修改表数据
	 * 
	 * @param	string		$table		表名称
	 * @param	array		$data		修改数据
	 * @param	object 		$db			数据模型对象
	 * @return	boolean
	 */    
    public function update($table, $data, $db = NULL){
    	
    	$db = $db == NULL ? $this->db : $db;
		return $db->update($table, $data);   	
    }
    	
    /**
     * 批量修改表
     * 
     * @param	string		$table		表名称
	 * @param	array		$data		修改数据
	 * @param	string		$key		唯一标示字段		
	 * @param	object 		$db			数据模型对象
	 * @return	boolean
     */
    public function updateBatch($table, $data, $key, $db = NULL){
    	
    	$db = $db == NULL ? $this->db : $db;
		return $db->update_batch($table, $data, $key);   	
    }

    /**
     * 添加数据
     * 
     * @param	string		$table		表名称
	 * @param	array		$data		添加数据
	 * @param	object 		$db			数据模型对象
	 * @return	boolean|integer
     */
    public function insert($table, $data, $db = NULL){
    	
    	$db = $db == NULL ? $this->db : $db;
		$query = $db->insert($table, $data);   	
		$id = $this->db->insert_id();
		return $id > 0 ? $id : $query;
    }
    
    /**
     * 批量添加数据
     * 
     * @param	string		$table		表名称
	 * @param	array		$data		添加数据
	 * @param	object 		$db			数据模型对象
	 * @return	boolean
     */
    public function insertBatch($table, $data, $db = NULL){
    	
    	$db = $db == NULL ? $this->db : $db;
		return $db->insert_batch($table, $data);
    }	
    
	/**
     * 列表分页
     *
     * @param  string  $table 表名
     * @param  array   $where 搜索条件
     * @return array
     */
    public function pagination($table, $total = NULL, $totalKey = '', $pageInfo = array()){
    	
    	$db = $this->db;//获取当前的数据库对象
    	$page_info = $pageInfo ? $pageInfo : $this->getPageInfo();
    	
    	if($total === NULL){
    		$total_db = clone $db;
    		$total = $this->count($table, $total_db, true, $totalKey);
    	}

        //一页显示的条数
        $number = (int)@$page_info['length'] > 0 ? $page_info['length'] : 25;
        
        //当前的页数
        $page = (int)@$page_info['start'] > 0 ? $page_info['start'] : 0;  
        
        //当前页要显示的数据
        $db->limit($number, $page);
        $data['data'] = $db->get($table)->result_array();
//         p($this->db->last_query());
        if( ! empty($page_info)) {
        	$data["sEcho"] = $page_info['sEcho'];
	        $data["iTotalRecords"] = $total;
	        $data["iTotalDisplayRecords"] = $total;
	        $data["iDisplayLength"] = $number;
	        $data["aaData"] = array();
        }
        
        return $data;
    }
    
    /**
     * 自定义分页
     * @param 	integer $table
     * @param 	integer $total
     * @return	array
     */
    public function customPage($table, $url = '', $total = null, $pageNumber = 10){
    	
    	$data = array();
    	if($total === NULL){
    		$total_db = clone $this->db;
    		$total = $this->count($table, $total_db);
    	}
    	
    	$page = get('per_page');
    	$page = (int) ($page ? $page : 0);
    	$this->db->limit($pageNumber, $page);
    	$data['list'] = $this->db->get($table)->result_array();
    	$this->load->library('pagination');
    	
    	foreach ($data['list'] as $k => $v){
    		$data['list'][$k]['order_number'] = $page + $k + 1;
    	}
    	
    	$config = array(
    				'base_url' => $url,
    				'total_rows' => $total,
    				'per_page' => $pageNumber,
    				'next_link' => '下一页',
    				'prev_link' => '上一页',
    				'cur_tag_open' => '<a class="active">',
    				'cur_tag_close' => '</a>',
    				'page_query_string' => true,
    				'display_pages' => true,
    	);
    	
    	$config = array(
    			'base_url' => $url,
    			'total_rows' => $total,
    			'per_page' => $pageNumber,
    			'next_link' => '下一页',
    			'prev_link' => '上一页',
    			'cur_tag_open' => '<a class="btn btn-default active">',
    			'cur_tag_close' => '</a>',
    			'num_tag_open' => '<a>',
    			'num_tag_close' => '</a>',
    			'page_query_string' => true,
    			'display_pages' => true,
    			'attributes' => array('class' => 'btn btn-default list-page'),
    	);
    	
    	$this->pagination->initialize($config);
    	$data['links'] = $this->pagination->create_links();
    	return $data;
    }
	
	/**
     * 获取到的datatable传入参数说明，这些参数将传入model进行数据库查找
     * 
     * @param  start   - 起始数据位置
     * @param  length  - 每页数据长度,通常和start一起运用进行ajax分页
     * @param  search  - 用于搜索得到的关键字
     * @param  sorting - 表示针对某一列进行排序，从0开始，例如sorting=1表示按第二列排序
     * @param  desc    - 前台传入的排序方式，值为"asc"或者"desc"
     * @param  sEcho   - 用于传回的值，可忽略
     * @return array
     */
    public function getPageInfo(){
		$dt_iDisplayLength = $this->input->get('dt_iDisplayLength');
		$iDisplayLength = $this->input->get('iDisplayLength');
		$start = $this->input->get('iDisplayStart');
		$sSearch = $this->input->get('sSearch');
		$iSortCol_0 = $this->input->get('iSortCol_0');
		
        $info = array(
        		   'start' => ( $start ? $start : 0),
        		   'length' =>  ! empty($dt_iDisplayLength) ? $dt_iDisplayLength : 
        				        ($iDisplayLength ? $iDisplayLength : 10),
        		   'search' => $sSearch ? $sSearch : null,
        		   'sorting' => $iSortCol_0,	
        		   'desc' =>  $this->input->get('sSortDir_0'),
        		   'sEcho' => $this->input->get('sEcho')
        		
        	);

        return $info;
    }
    
    /**
     * app端的分页
     */
    public function appPage($table, $page, $totalB = false, $limit = 0){
    	
    	$page = $page < 1 ? 1 : $page;
    	$pageSize = $limit || $limit < 0 ? $limit : 10;
    	$star = ($page - 1) * $pageSize;
    	$total = 0;
    	if($totalB){
    		$db = clone $this->db;		
    		$total = $this->count($table, $db);
    	}
    	$this->db->limit($pageSize, $star);
    	$data = $this->getAll($table);
    	$data = array(
    			  'list' => $data,
    			  'page' => $page
    			);
    	
    	if($totalB){
    		$data['total'] = $total;
    	}
    	
    	return $data;
    }
}